﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProfileRelationActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The profile relation actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Profiles
{
    using System;

    using NHibernate;

    using ServiceLibrary.Models.Base;
    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The profile relation actioner.
    /// </summary>
    public class ProfileRelationActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ProfileRelationActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public ProfileRelationActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create profile relation.
        /// </summary>
        /// <param name="type">
        /// The type.
        /// </param>
        /// <returns>
        /// The <see cref="Guid"/>.
        /// </returns>
        public Guid CreateProfileRelation(string type)
        {
            var rel = new ProfileRelation
                          {
                              ID = Guid.NewGuid(), 
                              Type = type, 
                              CreatedDateTime = DateTime.Now
                          };
            this.Session.Save(rel);

            this.Log.CreateLogEntry(
                rel.ID, string.Format("Tilhørsforhold '{0}', blev oprettet.", rel.Type), Enums.LogEntryType.ProfileRelation);

            return rel.ID;
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileRelationsResult"/>.
        /// </returns>
        public ProfileRelationsResult ExecuteSearchQuery(ProfileRelationsQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new ProfileRelationsResult();

            IQueryOver<ProfileRelation, ProfileRelation> search = this.Session.QueryOver<ProfileRelation>();
            this.ApplyDefaultCriterias(settings, search);

            result.TotalRowCount = search.RowCount();

            var relations = this.ApplySkipAndTake(settings, search.OrderBy(x => x.Type).Asc);
            
            foreach (ProfileRelation profileRelation in relations)
            {
                Services.Dto.ProfileRelation dto = Services.Dto.ProfileRelation.Convert(profileRelation);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get profile relation.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="ProfileRelation"/>.
        /// </returns>
        public ProfileRelation GetProfileRelation(Guid id)
        {
            var rel = this.Session.Get<ProfileRelation>(id);

            return rel;
        }

        #endregion
    }
}